<template>
  <div class="completion-confirmation">
    <div class="header">
      <router-link to="/main/wuye/process" class="back-btn">
        <i class="el-icon-arrow-left"></i>
        <span>返回</span>
      </router-link>
      <h2>完工确认</h2>
    </div>

    <div class="content">
      <el-tabs v-model="activeTab">
        <el-tab-pane label="待确认" name="pending">
          <el-table :data="pendingList" style="width: 100%">
            <el-table-column prop="repairNo" label="报修编号" width="180"></el-table-column>
            <el-table-column prop="applicant" label="报修人" width="120"></el-table-column>
            <el-table-column prop="repairStaff" label="维修人员" width="120"></el-table-column>
            <el-table-column prop="completionTime" label="完成时间" width="180"></el-table-column>
            <el-table-column prop="repairType" label="报修类型" width="120"></el-table-column>
            <el-table-column label="操作" width="200">
              <template slot-scope="scope">
                <el-button size="mini" @click="viewDetail(scope.row)">查看</el-button>
                <el-button size="mini" type="primary" @click="handleConfirmation(scope.row)">确认</el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-tab-pane>

        <el-tab-pane label="已确认" name="completed">
          <el-table :data="completedList" style="width: 100%">
            <el-table-column prop="repairNo" label="报修编号" width="180"></el-table-column>
            <el-table-column prop="applicant" label="报修人" width="120"></el-table-column>
            <el-table-column prop="confirmationTime" label="确认时间" width="180"></el-table-column>
            <el-table-column prop="satisfaction" label="满意度" width="120">
              <template slot-scope="scope">
                <el-rate v-model="scope.row.satisfaction" disabled></el-rate>
              </template>
            </el-table-column>
            <el-table-column label="操作" width="120">
              <template slot-scope="scope">
                <el-button size="mini" @click="viewDetail(scope.row)">查看</el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-tab-pane>
      </el-tabs>
    </div>

    <!-- 详情对话框 -->
    <el-dialog title="维修详情" :visible.sync="detailDialogVisible" width="60%">
      <div class="repair-detail" v-if="currentRepair">
        <el-descriptions title="基本信息" :column="2" border>
          <el-descriptions-item label="报修编号">{{ currentRepair.repairNo }}</el-descriptions-item>
          <el-descriptions-item label="报修人">{{ currentRepair.applicant }}</el-descriptions-item>
          <el-descriptions-item label="联系电话">{{ currentRepair.phone }}</el-descriptions-item>
          <el-descriptions-item label="报修位置">{{ currentRepair.location.join(' - ') }}</el-descriptions-item>
          <el-descriptions-item label="报修类型">{{ currentRepair.repairType }}</el-descriptions-item>
          <el-descriptions-item label="维修人员">{{ currentRepair.repairStaff }}</el-descriptions-item>
        </el-descriptions>

        <div class="detail-section">
          <h4>故障描述</h4>
          <p>{{ currentRepair.description }}</p>
        </div>

        <div class="detail-section">
          <h4>维修记录</h4>
          <el-timeline>
            <el-timeline-item
              v-for="(record, index) in currentRepair.repairInfo.records"
              :key="index"
              :timestamp="record.time">
              {{ record.content }}
              <p v-if="record.remark" class="repair-remark">备注：{{ record.remark }}</p>
            </el-timeline-item>
          </el-timeline>
        </div>

        <div class="detail-section" v-if="currentRepair.parts">
          <h4>更换配件</h4>
          <el-table :data="currentRepair.parts" border style="width: 100%">
            <el-table-column prop="name" label="配件名称" width="180"></el-table-column>
            <el-table-column prop="quantity" label="数量" width="120"></el-table-column>
            <el-table-column prop="price" label="单价" width="120">
              <template slot-scope="scope">
                {{ scope.row.price }}元
              </template>
            </el-table-column>
            <el-table-column prop="total" label="小计">
              <template slot-scope="scope">
                {{ scope.row.quantity * scope.row.price }}元
              </template>
            </el-table-column>
          </el-table>
        </div>

        <div class="detail-section" v-if="currentRepair.confirmation">
          <h4>确认信息</h4>
          <el-descriptions :column="2" border>
            <el-descriptions-item label="确认时间">{{ currentRepair.confirmation.time }}</el-descriptions-item>
            <el-descriptions-item label="满意度">
              <el-rate v-model="currentRepair.confirmation.satisfaction" disabled></el-rate>
            </el-descriptions-item>
          </el-descriptions>
          <p v-if="currentRepair.confirmation.feedback" class="confirmation-feedback">
            反馈意见：{{ currentRepair.confirmation.feedback }}
          </p>
        </div>
      </div>
    </el-dialog>

    <!-- 确认对话框 -->
    <el-dialog title="完工确认" :visible.sync="confirmationDialogVisible" width="40%">
      <el-form :model="confirmationForm" label-width="100px">
        <el-form-item label="维修结果">
          <el-radio-group v-model="confirmationForm.result">
            <el-radio label="已修复">已修复</el-radio>
            <el-radio label="未修复">未修复</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="维修满意度">
          <el-rate v-model="confirmationForm.satisfaction"></el-rate>
        </el-form-item>

        <el-form-item label="反馈意见">
          <el-input type="textarea" v-model="confirmationForm.feedback" rows="4"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="confirmationDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitConfirmation">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      activeTab: 'pending',
      pendingList: [
        {
          repairNo: 'BX202403001',
          applicant: '张三',
          phone: '13800138000',
          location: ['A栋', '1层', '101'],
          repairType: '水电维修',
          repairStaff: '李师傅',
          completionTime: '2024-03-20 15:30:00',
          description: '水管漏水',
          repairInfo: {
            records: [
              {
                time: '2024-03-20 14:00:00',
                content: '开始维修',
                remark: '检查故障点'
              },
              {
                time: '2024-03-20 15:30:00',
                content: '完成维修',
                remark: '更换水管并测试'
              }
            ]
          },
          parts: [
            { name: '水管', quantity: 2, price: 50 }
          ]
        }
      ],
      completedList: [
        {
          repairNo: 'BX202403002',
          applicant: '李四',
          confirmationTime: '2024-03-15 16:30:00',
          satisfaction: 5,
          confirmation: {
            time: '2024-03-15 16:30:00',
            satisfaction: 5,
            feedback: '维修及时，效果很好'
          }
        }
      ],
      detailDialogVisible: false,
      confirmationDialogVisible: false,
      currentRepair: null,
      confirmationForm: {
        result: '已修复',
        satisfaction: 5,
        feedback: ''
      }
    }
  },
  methods: {
    viewDetail(row) {
      this.currentRepair = row
      this.detailDialogVisible = true
    },
    handleConfirmation(row) {
      this.currentRepair = row
      this.confirmationForm = {
        result: '已修复',
        satisfaction: 5,
        feedback: ''
      }
      this.confirmationDialogVisible = true
    },
    submitConfirmation() {
      // TODO: 提交确认信息到后端
      const confirmation = {
        time: new Date().toLocaleString(),
        result: this.confirmationForm.result,
        satisfaction: this.confirmationForm.satisfaction,
        feedback: this.confirmationForm.feedback
      }

      this.currentRepair.confirmation = confirmation
      this.currentRepair.confirmationTime = confirmation.time
      this.currentRepair.satisfaction = confirmation.satisfaction

      // 从待确认列表移动到已确认列表
      const index = this.pendingList.findIndex(item => item.repairNo === this.currentRepair.repairNo)
      if (index > -1) {
        this.pendingList.splice(index, 1)
        this.completedList.unshift(this.currentRepair)
      }

      this.$message.success('确认提交成功')
      this.confirmationDialogVisible = false
    }
  }
}
</script>

<style lang="scss" scoped>
.completion-confirmation {
  padding: 20px;

  .header {
    margin-bottom: 30px;
    position: relative;
    text-align: center;

    .back-btn {
      position: absolute;
      left: 0;
      top: 50%;
      transform: translateY(-50%);
      text-decoration: none;
      color: #606266;
      display: flex;
      align-items: center;
    }

    h2 {
      margin: 0;
      font-size: 24px;
      color: #303133;
    }
  }

  .content {
    background: #fff;
    padding: 20px;
    border-radius: 8px;
    box-shadow: 0 2px 12px 0 rgba(0,0,0,0.1);
  }

  .detail-section {
    margin-top: 20px;

    h4 {
      margin: 0 0 10px;
      color: #303133;
    }

    p {
      margin: 0;
      color: #606266;
      line-height: 1.6;
    }

    .repair-remark {
      margin: 5px 0 0;
      font-size: 13px;
      color: #909399;
    }

    .confirmation-feedback {
      margin-top: 10px;
      font-style: italic;
    }
  }
}
</style> 